//
//  AddFeedbackViewController.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/31.
//

#import "AddFeedbackViewController.h"
#import "OneTabButView.h"
#import "FeedbackTypeView.h"
#import "FeedbackTitleView.h"
#import "FeedbackContentView.h"
#import "FeedbackImageView.h"
#import "LongFeedbackImageCell.h"
#import "LongFeedbackCameraCell.h"

@interface AddFeedbackViewController ()<UITableViewDelegate,UITableViewDataSource,OneTabButViewDelegate,FeedbackTypeViewDelegate,FeedbackContentViewDelegate,UITextFieldDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    FeedbackTypeView *_FeedbackTypeView;
    FeedbackTitleView *_FeedbackTitleView;
    FeedbackContentView *_FeedbackContentView;
    FeedbackImageView *_FeedbackImageView;
    
}

@property (nonatomic, strong) NSMutableArray *imagedataArr;
@end

@implementation AddFeedbackViewController
- (NSMutableArray *)imagedataArr
{
    if (!_imagedataArr) {
        _imagedataArr = [NSMutableArray array];
    }
    return _imagedataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navgationheadv.titlelabel.text = @"意见反馈";
    
    OneTabButView *tabbutview = [[OneTabButView alloc]initWithFrame:CGRectMake(0, APPheight-SafeAreaBottomHeight-65, APPwidth, 65)];
    tabbutview.delegate = self;
    [tabbutview.tabbut setTitle:@"确定" forState:UIControlStateNormal];
    [self.view addSubview:tabbutview];
    
    [self getData];
}

- (void)setupUI {
    [super setupUI];
    self.tableView.frame = CGRectMake(0, SafeAreaTopHight, APPwidth, APPheight-SafeAreaTopHight-65);
    
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 10, APPwidth, 595+(APPwidth - 62)/3.0+30)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    
    _FeedbackTypeView = [[FeedbackTypeView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 245)];
    _FeedbackTypeView.delegate = self;
    [HeaderV addSubview:_FeedbackTypeView];
    
    
    _FeedbackTitleView = [[FeedbackTitleView alloc]initWithFrame:CGRectMake(0, 245, APPwidth, 95)];
    _FeedbackTitleView.FeedbackTitleField.delegate = self;
    [HeaderV addSubview:_FeedbackTitleView];
    
    
    _FeedbackContentView = [[FeedbackContentView alloc]initWithFrame:CGRectMake(0, 340, APPwidth, 255)];
    _FeedbackContentView.delegate = self;
    [HeaderV addSubview:_FeedbackContentView];
    
    
    _FeedbackImageView = [[FeedbackImageView alloc]initWithFrame:CGRectMake(0, 605, APPwidth, (APPwidth - 62)/3.0+20)];
    _FeedbackImageView.myCollectionView.delegate = self;
    _FeedbackImageView.myCollectionView.dataSource = self;
    [HeaderV addSubview:_FeedbackImageView];
    
    self.tableView.tableHeaderView = HeaderV;
    
}

- (void)getData
{
    [self.imagedataArr addObject:[UIImage imageNamed:@"camera_addicon"]];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

#pragma mark - FeedbackTypeViewDelegate
-(void)ServicesBtnSelectedDelegate:(NSInteger)tag WithTitle:(NSString *)title
{
    NSLog(@"title ==%@",title);
}
#pragma mark - FeedbackContentViewDelegate
- (void)FeedbackContentViewDelegateWithContent:(NSString *)content
{
    NSLog(@"content ==%@",content);
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"textField.text ==%@",textField.text);
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imagedataArr.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.imagedataArr.count-1) {
        LongFeedbackCameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LongFeedbackCameraCell" forIndexPath:indexPath];
        return cell;
    }
    LongFeedbackImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LongFeedbackImageCell" forIndexPath:indexPath];
    [cell cellFillWithImage:self.imagedataArr[indexPath.row]];
    cell.deleteBut.tag = indexPath.row;
    [cell.deleteBut addTarget:self action:@selector(deleteButCheng:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.imagedataArr.count-1) {
        if (self.imagedataArr.count == 7) {
            [SVProgressHUD showErrorWithStatus:@"最多支持上传6张图片！"];
            return;
        }
        [LongHelper poppingBottomAlertWithReminderTitle:nil message:nil AndOneActionTitle:@"拍照" OneActionHandler:^(UIAlertAction *action)
         {
             [self takePhoto];
         } AndTwoActionTitle:@"从手机相册选择" TwoActionHandler:^(UIAlertAction *action) {
             [self selectPhoto];
         } AndCancelActionTitle:@"取消" NoActionHandler:^(UIAlertAction *action) {
             
         } ViewController:self];
    }
}


#pragma mark - 相机拍照
- (void)takePhoto
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;    //设置来源为相机
    imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;  //设置后置摄像头
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;   //是否显示裁剪框编辑
    [self presentViewController:imagePickerController animated:YES completion:nil];
}


#pragma mark - 相册选择相片
- (void)selectPhoto
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;    //设置来源为相册
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;   //是否显示裁剪框编辑
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.imagedataArr removeLastObject];
    [self.imagedataArr addObject:image];
    [self.imagedataArr addObject:[UIImage imageNamed:@"camera_addicon"]];
    [_FeedbackImageView.myCollectionView reloadData];
}


-(void)deleteButCheng:(UIButton *)but
{
    [self.imagedataArr removeObjectAtIndex:but.tag];
    [_FeedbackImageView.myCollectionView reloadData];
}


#pragma mark -OneTabButViewDelegate(提交)
- (void)OneTabButViewBtnSelectedDelegate:(NSInteger)tag
{
    
}

@end
